/*eslint-disable */
import './commonDocument.scss';
import PaginationDecorator from '../../core/controllers/Pagination.controller.decorator';
import storageApi from '../../core/storage/storage';

@Inject('$log', '$scope', '$http', '$state', 'documentService', 'ngDialog', 'ngDialogService')
class CommonDocumentCtrl extends PaginationDecorator() {
  constructor() {
    super(documentService);

    this.resource = {
      documentList: [],
    };

    this.$init();

    // 接受通知
    $scope.$on('childNotice', (e, m) => {
      this.mapParams.type = m.type;
      this.mapParams.orgId = m.orgId;
      this.resource.documentList = [];
      this.$paginationQuery();
    });
  }

  // 跳转到安全用表
  $goTable() {
    this.$state.go('securityTable', { name: this.$state.params.name, boxId: this.$state.params.boxId });
  }

  $init() {
    this.mapParams.orgId = this.$state.params.orgId;
    this.mapParams.type = this.$state.params.type;

    this.$setQueryCallback((res) => {
      this.resource.documentList = res.rows;
    });

    this.$paginationQuery();
  }

  getDownloadSrc(id) {
    return `/sgms/api/v1/common/downLoad/${id}`;
  }

  $addDocument(ctrl) {
    this.ngDialog.open({
      template: 'paAddDialog',
      className: 'ngdialog-theme-default pa-confirm-theme pa-confirm-self systemManage-add-dialog',
      controllerAs: 'vm',
      controller($rootScope, $scope, ngDialogService) {
        'ngInject';

        this.view = {
          attachmentFiles: [],
        };

        this.reqData = {
          orgId: ctrl.$state.params.orgId || storageApi.get('orgId'),
          documentName: '',
          documentNumber: '',
          compileTime: '',
          remark: '',
          attachmentIds: [],
          type: ctrl.mapParams.type,
        };

        this.submitForm = () => {
          this.reqData.attachmentIds = this.view.attachmentFiles.map(item => item.id);
          ctrl.documentService.save(this.reqData)
            .then((res) => {
              if (res.success) {
                ngDialogService.alertSuccess(res.message);
                $scope.closeThisDialog();
                ctrl.$paginationQuery();
              } else {
                ngDialogService.alertError(res.message);
              }
            });
        };
      },
    });
  }

  $editDocument(params, ctrl) {
    this.ngDialog.open({
      template: 'paEditDialog',
      className: 'ngdialog-theme-default pa-confirm-theme pa-confirm-self systemManage-add-dialog',
      controllerAs: 'vm',
      controller($scope, ngDialogService) {
        'ngInject';

        ctrl.documentService.get({ id: params.id })
          .then((res) => {
            if (res.success) {
              this.document = res.data;
              this.view = {
                attachmentFiles: this.document.files,
              };
              this.view.attachmentFiles.forEach((item) => {
                item.name = item.fileName;
              });
            } else {
              ngDialogService.alertError(res.message);
            }
          });

        this.submitEditForm = () => {
          this.document.attachmentIds = this.view.attachmentFiles.map(item => item.id);
          ctrl.documentService.update(this.document)
            .then((res) => {
              if (res.success) {
                ngDialogService.alertSuccess(res.message);
                $scope.closeThisDialog();
                ctrl.$paginationQuery();
              } else {
                ngDialogService.alertError(res.message);
              }
            });
        };
      },
    });
  }

  $delete(id) {
    this.ngDialogService.confirm('确定删除此数据吗？', id)
      .then((data) => {
        this.$log.debug(data);
        this.documentService
          .delete([id])
          .then((res) => {
            if (res.success) {
              this.ngDialogService.alertSuccess(res.message);
              this.$paginationQuery();
            }
          });
      });
  }


}

export default CommonDocumentCtrl;

